
// 找出顶层节点
function iteratorToRoot(node) {
    let lastNode = node;
    let count = 0;
    while (node && count < 6) {
        console.log('root', ++count, node.id());
        lastNode = node;
        node = node.parent();
    }
    return lastNode;
}


// 从根节点向下遍历所有节点
function iterateChildrenByRoot(root) {
    const queue = [root.id()];
    let count = 1;
    const set = new Set();
    while (queue.length !== 0) {
        // console.log('text', node.text(), 'desc', node.desc());
        const nodeId = queue.shift();
        console.log('nodeId', nodeId, queue.length);
        if (!nodeId) return;
        const node = id(nodeId).findOne();
        // console.log('count', count++, node.id(), set.size, queue.length);
        // console.log(queue.length);
        
        set.add(node.id());
        // if (node.clickable()) {
        //     console.log('text', node.text, 'desc', node.desc, 'id', node.fullId, 'className', node.className);
        // }
        const children = node.children();
        console.log('children.size', children.size());
        for(let i = 0, len = children.size(); i < len;i++) {
            const child = children[i];
            // console.log(i, child);
            iterateChildrenByRoot(children.get(i));
        }
        // children.get(0) && iterateChildrenByRoot(children.get(0));
        
        // children.forEach((child) => {
        //     if (child.id() !== null && !set.has(child.id())) {
        //         queue.push(child);
        //     }
        // });
    }
}

const obj = id('com.wm.dmall:id/left_menu').findOne();
// console.log('uiObj', obj.id());
// for (let k in obj) {
//     if ({}.hasOwnProperty.call(obj, k)) {
//         console.log('k', k, 'v', obj[k]);
//     }
// }
try {
    let root = iteratorToRoot(obj);
    // let root = id('android:id/content').findOne();
    iterateChildrenByRoot(root);
    console.log('迭代结束')   
} catch (error) {
    console.error('error', error);
}